<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentUploadPicture.class.php, $Author:  $ */



/** Component for upload group picture
 * @version $Id: PhpFox_ComponentUploadPicture.class.php 
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentUploadPicture extends BaseComponent
{
    /** Processing method
     */
    
    function process()
    {
        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('full'));
		
        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */
        
        $oGroupsGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGroupsGalleryService PhpFox_Mod_Groups_ServiceGallery */
        
         $oGroupsGalleryItem = $this->_oModule->getItem('GroupGallery');
        /* @var $oGroupsGalleryItem PhpFox_Mod_Group_ItemGroupGallery */
        
         $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */ 
         
        $oReq = &App::getRequest();
        /* @var $oReq Request */
        
        
        App::loadClass('file/Image');
        $oImage = &new file_Image(); 
        
        $oTpl = $this->_oModule->getTemplate();

        $iGroupId = $oReq->get('id', 0);
        $aErrors = array();
        
        if($iGroupId)
        {
            $aGroupData = $oGroupsService->getGroupData($iGroupId);
            $oUser = $this->getParam('user');
            $bIsOwner = $oGroupsService->isOwner($iGroupId, $oUser->aData['user']);
            $bIsMember = $oGroupsService->isMember($iGroupId, $oUser->aData['user']);

            if(!($bIsMember OR $oSecurityService->isStaff($oSecurityService->getCurrentUser())))
                App::gotoUrl('groups.view', array('id'=>$iGroupId));
            
            $bNeedGroupLeader = false;
            if(!($bIsOwner OR App::isStaff()) && $aGroupData['mem_image'] == 'No')
            {
                $bNeedGroupLeader = true;
            }
            else
            {
                if (array_key_exists('image', $_FILES))
				{
				    $iUsedSpace = $oAccountService->getUsedSpace($oSecurityService->getCurrentUserLogin());
			        $iAllowedSpace = $oAccountService->getAllowedSpace($oSecurityService->getCurrentUser());
			        $iMaxUploadSize = App::getSetting('max_upload_size') * 1024;
			        
			        if (($iAllowedSpace != 0) && (($iAllowedSpace - $iUsedSpace) < $iMaxUploadSize))
			        {
			            $iMaxUploadSize = $iAllowedSpace - $iUsedSpace;
			        }
			        
			         if ($iAllowedSpace)
	                {
	                    $oImage->aLimits['FILE_MAX_SIZE'] = $iMaxUploadSize;
	                }	                
	                
				    $oGroupsGalleryItem->aData = array
				    (
				    	'user'        => $oUser->aData['user'],
				        'group_id'    => $iGroupId,
				        'time'        => time(),
				        'title'       => $oReq->get('title',''),
				     );

	                $iImageId = $oGroupsGalleryItem->insert();
	                $sDestPath = App::getParam('groups.pic.path');
	                if ($oImage->upload('image', $sDestPath, $iImageId, 'jpg'))
	                {
	                    $oImage->loadInfo($sDestPath.$iImageId.'.jpg');
	                    $oImage->_checkLimits();
	                }
	                $this->addErrors($oImage->getErrors());    
   
	                if (!count($this->getErrors()))
	                {    
  	                    $oImage->makeThumbnail($sDestPath.'thumb/'.$iImageId.'.jpg', App::getSetting('upload_set_size_thumb'), App::getSetting('upload_set_size_thumb'));
  	                    
		                $oImage->makeThumbnail($sDestPath.$iImageId.'_view.jpg', App::getParam('upload_set_size_view'), App::getParam('upload_set_size_view'));
 	                    if(!App::getParam('bSaveOriginalImages'))
	                    {
	                    	unlink($sDestPath . $iImageId.'.jpg');
	                    }
	                    
	                	App::gotoUrl('public.groups.gallery',array('id'=>$iGroupId)); 
	                }
	                else
	                {
	                    $oGroupsGalleryService->deleteItem($iImageId);
	                    $aErrors = $oImage->getErrors();
	                } 
				} 
            }
            
	        $oTpl->assign(array(
	            'aErrors'        => $aErrors,
	            'aGroupData'     => $aGroupData,
	            'bNeedGroupLeader'=> $bNeedGroupLeader,
	            'aGroupData'     => $aGroupData,
	            'iMaxImageSize' => App::getSetting('max_upload_size'),
	        ));
	        $sHtml = $oTpl->fetch('UploadPicture.html');
        }
        return $sHtml;
    }
}
?>